package com.dongzili.demo.common.vo;

import com.baomidou.mybatisplus.annotation.TableId;
import com.dongzili.demo.common.utils.ReflectUtils;
import lombok.Getter;

import java.lang.reflect.Field;

@Getter
public class ReflectClassInfo <T> {

    private final Class<T> tClass;
    private final String primaryColumnKey;
    private final String primaryFieldKey;
    private final Field primaryField;
    private final String tableName;

    public ReflectClassInfo(Class<T> tClass) {
        this.tClass = tClass;

        this.tableName = ReflectUtils.getTableName(tClass);
        Field primaryField = ReflectUtils.getPrimaryField(tClass);
        this.primaryField = primaryField;
        if(primaryField != null) {
            if(primaryField.isAnnotationPresent(TableId.class)){
                this.primaryColumnKey = primaryField.getAnnotation(TableId.class).value();
            }else{
               this.primaryColumnKey = primaryField.getName();
            }
            this.primaryFieldKey = primaryField.getName();
        }else{
            this.primaryColumnKey = null;
            this.primaryFieldKey = null;
        }

    }
}
